(*
  Img module
  -----------------------------------
  Authors:
  Antoine Jackson

  -----------------------------------
  This modules provides advanced sdl function
  It also provides functions to get the color
  list from the original image
  

*)


(*sdl initialization*)
let sdl_init () =
  begin 
    Sdl.init [`EVERYTHING];
    Sdlevent.enable_events Sdlevent.all_events_mask;
  end


let get_dims img =
  ((Sdlvideo.surface_info img).Sdlvideo.w, 
   (Sdlvideo.surface_info img).Sdlvideo.h)


let rec wait_key () =
  let time = Sdltimer.get_ticks () in
  let e = Sdlevent.wait_event () in
  match e with
    | Sdlevent.KEYDOWN {Sdlevent.keysym=Sdlkey.KEY_ESCAPE} -> 
	print_string ("Program terminated at "^
			 string_of_int (time / 1000)^" seconds.\n");
	()
    | _ -> wait_key ()


(*
Creates a bidimensional vector || color , int ||
This vector contains for each color the 
corresponding height
*)

let rec add (color,height) = function 
((0,0,0),_)::l -> l
  | [] -> (color,height)::[]
  | (c,h)::l when c = color -> (c,h)::l
  | (c,h)::l -> (c,h)::(add (color,height) l)
    
(*
type color_list = 
{ mutable col : Sdlvideo.color list }
*)
let color_find img step = 
  begin
    let (w,h) = get_dims img in
    let l = ref [] in
    for x=step*10 to w-step do
      for y=step*10 to h-step do
	let (r,g,b) = Sdlvideo.get_pixel_color img x y in
	l := add ((r,g,b),(float)g) !l;
      done
    done;
    !l
  end

